/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { constants } from './constants.js'
import { Node } from './Node.js'
// "use strict";

var __importDefault = this && this.__importDefault || function (mod) {
    return mod && mod.__esModule ? mod : { "default": mod };
};

const constants_1 = constants;
const Node_1 = __importDefault(Node);
class Decorator extends Node {
    constructor({ config = {}, props } = { config: {} }) {
        super(props);
        this.nodeType = 'Decorator';
        this.setConfig(config);
    }
    decorate(run, blackboard, config) {
        // This method should be overridden to make it useful
        return run(run, blackboard, config);
    }
    run(blackboard, { introspector, rerun, registryLookUp = x => x, config } = {}) {
        // run(blackboard, { introspector, rerun, registryLookUp = (x) => x, ...config } = {}) {
        if (!rerun) this.blueprint.start(blackboard);
        let runCount = 0;
        const result = this.decorate(() => {
            ++runCount;
            return registryLookUp(this.blueprint.node).run(blackboard, {
                // ...config,
                config,
                rerun,
                introspector,
                registryLookUp
            });
        }, blackboard, this.config);
        if (result !== constants_1.RUNNING) {
            this.blueprint.end(blackboard);
        }
        if (introspector) {
            introspector.wrapLast(runCount, this, result, blackboard);
        }
        return result;
    }
    setConfig(config) {
        this.config = config;
    }
}
// exports.default = Decorator;
export { Decorator }